package com.hhh.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.Dto.OrdersDto;
import com.hhh.ExceptionHandle.AddressBookException;
import com.hhh.ExceptionHandle.UserException;
import com.hhh.Mapper.OrderMapper;
import com.hhh.Pojo.*;
import com.hhh.Service.AddressBookService;
import com.hhh.Service.OrderDetailSeivce;
import com.hhh.Service.ShoppingCartSerivce;
import com.hhh.Service.UserSerivce;
import org.apache.catalina.filters.ExpiresFilter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.function.Function;

@Service
public class OrderSerivce implements com.hhh.Service.OrderSerivce {
    @Autowired
    OrderMapper orderMapper;

    @Autowired
    ShoppingCartSerivce shoppingCartSerivce;

    @Autowired
    OrderDetailSeivce orderDetailSeivce;

    @Autowired
    UserSerivce userSerivce;

    @Autowired
    AddressBookService addressBookService;

    //    添加订单数据
    @Override
    @Transactional
    public Integer Insert(Orders orders) {
//        核对用户信息
        User user = new User();
        user.setId(orders.getUserId());
        User tempuser = userSerivce.SelectById(user);
        if (tempuser == null)
            throw new UserException("未登录用户，请登录");

//        核对地址
        AddressBook addressBook = addressBookService.GetDefaultAddress();
        if (addressBook == null)
            throw new AddressBookException("地址不正确");

        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(orders.getUserId());
        List<ShoppingCart> shoppingCartList = shoppingCartSerivce.SelectList(shoppingCart);
        //        获取订单创建时间
        LocalDateTime time = shoppingCartList.get(shoppingCartList.size() - 1).getCreateTime();
        orders.setOrderTime(time);
//        获取订单金额
        BigDecimal bigDecimal = new BigDecimal(0);
        int num;
        for (num = 0; num < shoppingCartList.size(); num++) {
            BigDecimal amount = shoppingCartList.get(num).getAmount();
            bigDecimal = bigDecimal.add(amount);
        }
        orders.setAmount(bigDecimal);
        orders.setPhone(tempuser.getPhone());
        orders.setUserName(tempuser.getName());
        orders.setConsignee(tempuser.getName());

        orders.setAddress(addressBook.getDetail());

//        添加订单
        int insertnum = orderMapper.insert(orders);

//       这里有问题
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", orders.getUserId()).eq("status", 1);
        Orders temporders = orderMapper.selectOne(wrapper);
        long orderid = temporders.getId();
//        获取订单具体信息
        List<OrderDetail> detailList = shoppingCartList.stream().map(new Function<ShoppingCart, OrderDetail>() {
            @Override
            public OrderDetail apply(ShoppingCart shoppingCart) {
                OrderDetail orderDetail = new OrderDetail();
                orderDetail.setAmount(shoppingCart.getAmount());
                orderDetail.setNumber(shoppingCart.getNumber());
                orderDetail.setDishId(shoppingCart.getDishId());
                orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
                orderDetail.setSetmealId(shoppingCart.getSetmealId());
                orderDetail.setImage(shoppingCart.getImage());
                orderDetail.setName(shoppingCart.getName());
                orderDetail.setOrderId(orderid);
                return orderDetail;
            }
        }).toList();
//        添加订单具体数据
        orderDetailSeivce.Insert(detailList);

//        删除购物车里的数据
        shoppingCartSerivce.Delete(shoppingCart.getUserId());

//        修改订单状态
        Update(temporders);
        return insertnum;
    }


    //    查询订单
    @Override
    public IPage<OrdersDto> SelectPage(long page, long pageSize, long id) {

        IPage<OrdersDto> ordersDtoIPage = new Page<>();
        IPage<Orders> iPage = new Page<>();
        iPage.setSize(pageSize);
        iPage.setCurrent(page);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", id);
        IPage<Orders> ordersIPage = orderMapper.selectPage(iPage, wrapper);
        BeanUtils.copyProperties(ordersIPage, ordersDtoIPage, "records");

        List<OrdersDto> ordersDtoList = ordersIPage.getRecords().stream().map(new Function<Orders, OrdersDto>() {
            @Override
            public OrdersDto apply(Orders orders) {
                OrdersDto ordersDto = new OrdersDto();
                BeanUtils.copyProperties(orders, ordersDto);
                ordersDto.setOrderDetails(orderDetailSeivce.SelectList(orders.getId()));
                return ordersDto;
            }
        }).toList();
        ordersDtoIPage.setRecords(ordersDtoList);
        return ordersDtoIPage;
    }

    //    修改订单状态
    @Override
    public Integer Update(Orders orders) {
        orders.setStatus(2);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("id", orders.getId());
        return orderMapper.update(orders, wrapper);
    }
}
